package reviewAlgorithms.tree;

public class KthSmallest {
    static int count = 1;
    static int res = 0;
    public static void main(String[] args) {
        TreeNode root = new TreeNode(2);
        TreeNode root1 = new TreeNode(1);
        TreeNode root2 = new TreeNode(3);
        root.left = root1;
        root.right = root2;
        kthSmallest(root,1);
    }
    public static int kthSmallest(TreeNode root, int k) {
        if (root == null) {
            return -1;
        }
        getRes(root,k);
        return res;
    }

    private static void getRes(TreeNode root, int k) {
        if (root == null){
            return;
        }
        getRes(root.left,k);
        if (count == k){
            res = root.val;
        }
        if (count > k){
            return;
        }
        count ++;
        getRes(root.right,k);
    }
}
